<?php
!defined("PHOTO_ADMIN") && exit("ACCESS DENIED");
$job = empty($_GET['job']) ? "list" : $_GET['job'];

$url = "index.php?do=comment";
if($job=="list"){
	$page = empty($_GET['page']) ? 1 : $_GET['page'];
	$pageSize = empty($_GET['pageSize']) ? 20 : $_GET['pageSize'];
	$title = $_POST['title'];
	!$title && $title =  $_GET['title'];

	$author = $_POST['author'];
	!$author && $author =  $_GET['author'];

	$keyword = $_POST['keyword'];
	!$keyword && $keyword =  $_GET['keyword'];
	$where = " WHERE 1=1";
	if(!empty($keyword)){
		$where .= " AND c.content LIKE '%$keyword%'";
	}
	if(!empty($title)){
		$where .= " AND title LIKE '%$title%'";
	}
	if(!empty($author)){
		$where .= " AND author LIKE '%$author%'";
	}
	
	$limit = " LIMIT ".($page-1)*$pageSize.",".$pageSize;

	$sql = "SELECT c.id,c.content,c.createtime,ifhide,title,imageid from ".tname('comment')." c LEFT JOIN  ".tname('image')." p ON c.imageid=p.id  $where ORDER BY c.createtime DESC $limit";

	$query = $db->query($sql);
	$list = $db->fetch_all($query);
	
	$countSql = "SELECT COUNT(*) AS amount from ".tname('comment')." c LEFT JOIN  ".tname('image')." p ON c.imageid=p.id  $where ORDER BY c.createtime DESC "; 
	$countRs = $db->get_one($countSql);
	$pagetag = multi($countRs['amount'],$pageSize,$page,"index.php?do=comment&job=list&keyword=".urlencode($keyword));
	

	include atemplate("do_comment_list");
}else if($job=="hide"){
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	}else{
		$db->query('update '.tname('comment').' set ifhide=1 where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('屏蔽评论成功',$url);
		}else {
			adminmessage('屏蔽评论失败',$url);
		}
	}

}else if($job=="canclehide"){
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	}else{
		$db->query('update '.tname('comment').' set ifhide=0 where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('解除屏蔽评论成功',$url);
		}else {
			adminmessage('解除屏蔽评论失败',$url);
		}
	}

}else if($job=="delete"){
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	}else{
		$db->query('DELETE FROM '.tname('comment').' where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除图片成功',$url);
		}else {
			adminmessage('删除图片失败',$url);
		}
	}
}
?>